﻿// -----------------------------------------------------------------------
// <copyright file="NguoiDungDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class NguoiDungDataAccess
    {
        public bool InsertNguoiDung(NguoiDungEntity obj)
        {
            try
            {
                string sql = "spNguoiDung_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "UserName", DbType.String, obj.UserName);
                db.AddInParameter(cmd, "Password", DbType.String, obj.Password);
                db.AddInParameter(cmd, "MaNhom", DbType.String, obj.MaNhom);
                db.AddInParameter(cmd, "TenNguoiDung", DbType.String, obj.TenNguoiDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertNguoiDung", ex.Message); throw ex; }
        }

        public bool EditNguoiDung(NguoiDungEntity obj)
        {
            try
            {
                string sql = "spNguoiDung_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "UserName", DbType.String, obj.UserName);
                db.AddInParameter(cmd, "Password", DbType.String, obj.Password);
                db.AddInParameter(cmd, "MaNhom", DbType.String, obj.MaNhom);
                db.AddInParameter(cmd, "TenNguoiDung", DbType.String, obj.TenNguoiDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateNguoiDung", ex.Message); throw ex; }
        }

        public bool DeleteNguoiDung(string userName)
        {
            try
            {
                string sql = "spNguoiDung_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "UserName", DbType.String, userName);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteNguoiDung", ex.Message); throw ex; }
        }

        public NguoiDungEntity GetNguoiDung(string userName)
        {
            try
            {
                NguoiDungEntity obj = null;
                string sql = "spNguoiDung_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "UserName", DbType.String, userName);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new NguoiDungEntity();
                        obj.UserName = reader["UserName"].ToString();
                        obj.Password = reader["Password"].ToString();
                        obj.MaNhom = reader["MaNhom"].ToString();
                        obj.TenNguoiDung = reader["TenNguoiDung"].ToString();
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetNguoiDung", ex.Message); throw ex; }
        }

        public List<NguoiDungEntity> GetNguoiDungList()
        {
            try
            {
                List<NguoiDungEntity> list = new List<NguoiDungEntity>();
                NguoiDungEntity obj = null;
                string sql = "spNguoiDung_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new NguoiDungEntity();
                        obj.UserName = reader["UserName"].ToString();
                        obj.Password = reader["Password"].ToString();
                        obj.MaNhom = reader["MaNhom"].ToString();
                        obj.TenNguoiDung = reader["TenNguoiDung"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetNguoiDungList", ex.Message); throw ex; }
        }

    }
}